﻿' ******************************************************************************
' ** 
' **  Yahoo! Managed
' **  Written by Marius Häusler 2011
' **  It would be pleasant, if you contact me when you are using this code.
' **  Contact: YahooFinanceManaged@gmail.com
' **  Project Home: http://code.google.com/p/yahoo-finance-managed/
' **  
' ******************************************************************************
' **  
' **  Copyright 2011 Marius Häusler
' **  
' **  Licensed under the Apache License, Version 2.0 (the "License");
' **  you may not use this file except in compliance with the License.
' **  You may obtain a copy of the License at
' **  
' **    http://www.apache.org/licenses/LICENSE-2.0
' **  
' **  Unless required by applicable law or agreed to in writing, software
' **  distributed under the License is distributed on an "AS IS" BASIS,
' **  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' **  See the License for the specific language governing permissions and
' **  limitations under the License.
' ** 
' ******************************************************************************


Namespace Base

    ''' <summary>
    ''' Provides information about a download process.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ConnectionInfo

        Private mException As Net.WebException
        Private mTimeout As Integer = 0
        Private mSizeInBytes As Integer = 0
        Private mStartTime, mEndTime As DateTime
        Private mTimeSpan As TimeSpan

        ''' <summary>
        ''' Indicates the connection status.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property State() As ConnectionState
            Get
                If mException Is Nothing Then
                    Return ConnectionState.Success
                Else
                    Select Case mException.Status
                        Case WebExceptionStatus.RequestCanceled : Return ConnectionState.Canceled
                        Case WebExceptionStatus.Timeout : Return ConnectionState.Timeout
                        Case Else : Return ConnectionState.ErrorOccured
                    End Select
                End If
            End Get
        End Property
        ''' <summary>
        ''' If an exception occurs during download process, the exception object will be stored here. If no exception occurs, this property is null/Nothing.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Exception() As Net.WebException
            Get
                Return mException
            End Get
        End Property
        ''' <summary>
        ''' The size of downloaded data in bytes.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property SizeInBytes() As Integer
            Get
                Return mSizeInBytes
            End Get
        End Property
        ''' <summary>
        ''' The setted timeout for download process in milliseconds.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Timeout() As Integer
            Get
                Return mTimeout
            End Get
        End Property
        ''' <summary>
        ''' The start time of download process, independent to individual preparation of passed parameters for start downloading.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property StartTime() As DateTime
            Get
                Return mStartTime
            End Get
        End Property
        ''' <summary>
        ''' The end time of the download process, independent to post-processing actions like parsing.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property EndTime() As DateTime
            Get
                Return mEndTime
            End Get
        End Property
        ''' <summary>
        ''' The time span of start and end time.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property TimeSpan() As TimeSpan
            Get
                Return mTimeSpan
            End Get
        End Property
        Public ReadOnly Property KBPerSecond() As Double
            Get
                If Me.TimeSpan.TotalMilliseconds <> 0 Then : Return mSizeInBytes / Me.TimeSpan.TotalMilliseconds
                Else : Return 0
                End If
            End Get
        End Property

        Friend Sub New(ByVal exception As Net.WebException, ByVal timeout As Integer, ByVal size As Integer, ByVal startTime As DateTime, ByVal endTime As DateTime)
            mException = exception
            mTimeout = timeout
            mSizeInBytes = size
            mEndTime = endTime
            mStartTime = startTime
            mTimeSpan = mEndTime - mStartTime
        End Sub

    End Class

End Namespace